package reduce.fz.com.reduce.activity;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.alibaba.fastjson.TypeReference;

import java.io.File;

import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.abstracts.AbResponseListener;
import reduce.fz.com.reduce.bean.User;
import reduce.fz.com.reduce.bean.result.Result;
import reduce.fz.com.reduce.constant.ParamConst;
import reduce.fz.com.reduce.custom.LoadingDialog;
import reduce.fz.com.reduce.custom.view.RoundNetworkImageView;
import reduce.fz.com.reduce.service.preference.UserPreference;
import reduce.fz.com.reduce.util.HttpUtils;
import reduce.fz.com.reduce.util.MD5;
import reduce.fz.com.reduce.util.base.CropParam;
import reduce.fz.com.reduce.util.oss.Oss;
import reduce.fz.com.reduce.util.photo.PhotoSelect;
import reduce.fz.com.reduce.util.photo.PhotoTake;
import reduce.fz.com.reduce.util.photo.SelectParam;

public class RegisterSecond extends BaseToolBarActivity implements View.OnClickListener {
    //传入参数(手机号)
    private String mobile;
    //传入参数(密码)
    private String password;
    //拍照
    private RelativeLayout mRlTakePhoto;
    //昵称
    private EditText mEtNickname;
    //单选男
    private ToggleButton mTbMale;
    //单选女
    private ToggleButton mTbFemale;
    //完成
    private TextView mRegisterComplete;

    private LinearLayout mLlTakePhoto;
    private LinearLayout mLlSelectPhoto;
    private User user = new User();
    private Uri imageUri;

    private PhotoTake photoTake;
    private PhotoSelect photoSelet;

    private RoundNetworkImageView mIvUserHeader;

    private PopupWindow popupWindow;

    private View photoView;


    @Override
    public void initWidget() {
        mRlTakePhoto = (RelativeLayout) findViewById(R.id.rl_take_photo);
        mEtNickname = (EditText) findViewById(R.id.et_nick_name);
        mTbFemale = (ToggleButton) findViewById(R.id.tb_female);
        mTbMale = (ToggleButton) findViewById(R.id.tb_male);
        mRegisterComplete = (TextView) findViewById(R.id.register_complete);
        mIvUserHeader = (RoundNetworkImageView) findViewById(R.id.iv_user_header);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register_second);
        //获取用户名
        mobile = getIntent().getStringExtra(ParamConst.MOBILE);
        //获取密码
        password = getIntent().getStringExtra(ParamConst.PASSWORD);
        initWidget();
        initTakePhoto();
        initListener();

    }

    public void showWindow()
    {
        popupWindow = new PopupWindow(photoView, ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.showAtLocation(mRlTakePhoto, Gravity.CENTER, 0, 0);
    }
    public void dimissWindow()
    {
        if(popupWindow!=null&&popupWindow.isShowing())
        {
            popupWindow.dismiss();
        }
    }



    public void initTakePhoto() {
        photoView = LayoutInflater.from(this.getBaseContext()).inflate(R.layout.view_modify_header, null);
        mLlTakePhoto = (LinearLayout) photoView.findViewById(R.id.ll_take_photo);
        mLlSelectPhoto = (LinearLayout) photoView.findViewById(R.id.ll_select_photo);
        mLlTakePhoto.setOnClickListener(this);
        mLlSelectPhoto.setOnClickListener(this);
    }

    public void takePhoto() {
        photoTake = PhotoTake.instance(this);
        imageUri = photoTake.takePhoto();

    }

    public void selectPhoto() {
        SelectParam param = new SelectParam();
        param.selectMax = 1;
        photoSelet = PhotoSelect.instance(this, param);
    }


    @Override
    public void initListener() {
        mRlTakePhoto.setOnClickListener(this);
        mLlSelectPhoto.setOnClickListener(this);
        mLlTakePhoto.setOnClickListener(this);
        mRegisterComplete.setOnClickListener(this);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case PhotoTake.TAKE_PIC:
                CropParam param = new CropParam(2, 1, 300, 600, false);
                imageUri = photoTake.cropImageUri(imageUri, param);
                break;
            case PhotoTake.CROP_PIC:
                setAvatar();
                break;
            case PhotoSelect.SELECT_PIC:
                String[] paths = photoSelet.getSelectPaths(data);
                imageUri = Uri.fromFile(new File(paths[0]));
                setAvatar();
                break;

            default:
                break;
        }
    }

    //设置头像
    public void setAvatar() {
        mIvUserHeader.setImageURI(imageUri);
    }

    public void uploadAvatar() {
        Oss.instance(this).uploadPic(imageUri.getPath(), new Oss.OssUploadResponse() {

            @Override
            public void onSuccess(String objectKey, String picUrl) {
                user.avatar = picUrl;
            }

            @Override
            public void onFailed(String objectKey) {

            }
        });
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case (R.id.ll_take_photo):
                takePhoto();
                break;
            case (R.id.ll_select_photo):
                selectPhoto();
                break;
            case (R.id.register_complete):
                register();
                break;
            case R.id.rl_take_photo:
                showWindow();
            default:
                break;

        }
    }


    public void register() {
        comParam();
        requestRegister();
    }


    public void requestRegister() {
       final LoadingDialog dialog= LoadingDialog.create(this);
        dialog.show();
        HttpUtils.jsonPost("", user, new AbResponseListener<User>(new TypeReference<Result<User>>() {
        }) {

            @Override
            public void responseInfo(Result<User> data) {
                dialog.dismiss();
                user = data.getData();
                //保存用户信息到本地
                saveUserInfo();
                forward();
            }
        });
    }
    public void saveUserInfo() {
        UserPreference.saveUser(user);
    }


    public void comParam() {
        user.mobile = mobile;
        user.password = MD5.md5(password);
        user.nickname=mEtNickname.getText().toString();
    }

    public void forward() {
        Intent intent = new Intent(this, DoorActivity.class);
        startActivity(intent);
    }


}
